package com.supermap.dataservice.webservice;


import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.*;

/**
 * @author Aqua 2017-5-26
 * @Filename HttpClient.java
 * @Description 调用webservice 通过soap方式
 * @email 1301584033@qq.com
 */
public class HttpClient {

    /**
     * 访问服务
     *
     * @param wsdl   wsdl地址
     * @param method 方法名
     * @param params 参数
     * @return String
     * @throws Exception
     */
    public synchronized static String accessService(String wsdl, String method, Map<String, String> params) throws Exception {
        //创建一个默认的httpclient
        CloseableHttpClient httpclient = HttpClients.createDefault();
        //创建一个 httppost请求
        HttpPost httpPost = new HttpPost(wsdl);
        //给请求复制参数
        httpPost.setEntity(new StringEntity(getSoap(params, method).toString()));
        //给httpclient装载参数
        CloseableHttpResponse response = httpclient.execute(httpPost);
        //发送请求并接受返回值xml的字符串
        String result = EntityUtils.toString(response.getEntity());
        response.close();
        return result;


    }

    /**
     * 获取凭借的Soap
     *
     * @param method 方法名
     * @param param  参数
     * @return StringBuffer
     */
    public static StringBuffer getSoap(Map<String, String> param, String method) {
        //拼接SOAP  
        StringBuffer soapRequestData = new StringBuffer("");
        soapRequestData.append("<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:wsdllog_add\">");
        soapRequestData.append("<soapenv:Header/>");
        soapRequestData.append("<soapenv:Body>");
        soapRequestData.append("<" + method + ">");
        soapRequestData.append(getParam(param).toString());
        soapRequestData.append("</" + method + ">");
        soapRequestData.append("</soapenv:Body>");
        soapRequestData.append("</soapenv:Envelope>");
        return soapRequestData;
    }




    /**
     * 获取一个拼接参数的字符串
     *
     * @param method 方法名
     * @param params 参数
     * @return String
     */
    public static String getParam(Map<String, String> params) {
        String param = "";
        if (params != null) {
            Iterator<String> it = params.keySet().iterator();
            while (it.hasNext()) {
                String str = it.next();
                param += "<" + str + ">";
                param += params.get(str);
                param += "</" + str + ">";
            }
        }
        return param;
    }


    public static List<String> getXml(String date) throws Exception {
        Map<String, String> param = new HashMap<String, String>();
        param.put("uName ", "cqqxjkys");
        param.put("uPwd", "cqqxjkys@cqmb.121");
        param.put("sta ", "MS");
        param.put("date", date);
        param.put("elemental", "*");
        String wsdl = "http://183.230.183.20:8686/axis2/services/Meteorologicalservices?wsdl";
        String method = "MeteoForecastDataGetDaily";
        String response =accessService(wsdl, method, param);
      return ParseXml.getXml(response);
    }

}
